<?php
require_once 'auth.php';
requireAdmin();

$conn = dbConnect();

// 处理搜索参数
$searchType = $_GET['type'] ?? '';
$searchUser = $_GET['user_id'] ?? '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;

// 构建基础查询
$sql = "SELECT 
    l.id,
    u.username,
    l.operation_type,
    l.target_id,
    l.details,
    l.operate_at 
FROM operation_logs l
LEFT JOIN users u ON l.user_id = u.id
WHERE 1=1";

$conditions = [];
$types = '';
$params = [];

// 添加过滤条件
if (!empty($searchType)) {
    $conditions[] = "l.operation_type = ?";
    $types .= 's';
    $params[] = $searchType;
}
if (!empty($searchUser)) {
    $conditions[] = "l.user_id = ?";
    $types .= 'i';
    $params[] = (int)$searchUser;
}

if (!empty($conditions)) {
    $sql .= " AND " . implode(" AND ", $conditions);
}

$sql .= " ORDER BY l.operate_at DESC LIMIT ?, ?";
$types .= 'ii';
$params[] = $offset;
$params[] = $perPage;

$stmt = $conn->prepare($sql);
if ($stmt) {
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $logs = $stmt->get_result();
} else {
    die("查询准备失败: " . $conn->error);
}

// 获取总记录数
$countSql = "SELECT COUNT(*) as total FROM operation_logs l WHERE 1=1";
$countTypes = '';
$countParams = [];

if (!empty($searchType)) {
    $countSql .= " AND l.operation_type = ?";
    $countTypes .= 's';
    $countParams[] = $searchType;
}
if (!empty($searchUser)) {
    $countSql .= " AND l.user_id = ?";
    $countTypes .= 'i';
    $countParams[] = (int)$searchUser;
}

$countStmt = $conn->prepare($countSql);
if ($countStmt) {
    if (!empty($countParams)) {
        $countStmt->bind_param($countTypes, ...$countParams);
    }
    $countStmt->execute();
    $totalResult = $countStmt->get_result();
    $totalItems = $totalResult->fetch_assoc()['total'];
    $totalPages = ceil($totalItems / $perPage);
} else {
    die("总数查询准备失败: " . $conn->error);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>操作日志</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>

<body class="layout-fluid layout-vertical">
    <?php include 'navbar.php'; ?>
    <div class="page">
        <div class="container-fluid">
            <div class="card mt-3">
                <div class="card-header">
                    <h3 class="card-title">操作日志</h3>
                    <form class="ms-auto d-flex gap-2" method="get">
                        <div class="input-group"> <!-- 添加input-group包装 -->
                            <input type="text" name="user_id" class="form-control" placeholder="用户ID" value="<?= htmlspecialchars($searchUser) ?>">
                        </div>
                        <select name="type" class="form-select">
                            <option value="">全部类型</option>
                            <option value="add" <?= $searchType === 'add' ? 'selected' : '' ?>>新增</option> <!-- 修复selected判断 -->
                            <option value="edit" <?= $searchType === 'edit' ? 'selected' : '' ?>>更新</option>
                            <option value="delete" <?= $searchType === 'delete' ? 'selected' : '' ?>>删除</option>
                        </select>
                        <button type="submit" class="btn btn-primary">搜索</button>
                    </form>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-vcenter table-hover">
                            <thead>
                                <tr>
                                    <th>操作人</th>
                                    <th>操作类型</th>
                                    <th>目标ID</th>
                                    <th>详情</th>
                                    <th>时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($log = $logs->fetch_assoc()): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($log['username']) ?></td>
                                        <td><?= htmlspecialchars($log['operation_type']) ?></td>
                                        <td><?= $log['target_id'] ?></td>
                                        <td><?= htmlspecialchars($log['details']) ?></td>
                                        <td><?= date('Y-m-d H:i', strtotime($log['operate_at'])) ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php include 'pagination.php'; ?>
                </div>
            </div>
        </div>
    </div>
    <script src="/dist/js/tabler.min.js"></script>
</body>

</html>